﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using WFNetLib;
using WFNetLib.PacketProc;

namespace 点滴架调试
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            CheckForIllegalCrossThreadCalls = false;
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbCom.Items.Clear();
            cbCom.Items.AddRange(ports);
            cbCom.SelectedIndex = 0;
            ComRxPacket = new CP1616Packet(0x03,0x0001);
        }
        CP1616Packet ComRxPacket;
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            toolStripButton1.Checked = true;
            toolStripButton1.Enabled = false;
            toolStripButton1.Text = "跟踪中---------------------";
            Com.PortName = cbCom.Text;
            Com.Open();

            byte[] txbuffer = CP1616Packet.MakeCP1616Packet(0x03, 0x0001, 0x0);
            Com.Write(txbuffer, 0, txbuffer.Length);
            index = 0;
            timer1.Enabled = true;
        }
        int index;
        private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int rxn = Com.BytesToRead;
            while (rxn != 0)
            {
                rxn--;
                byte rx = (byte)Com.ReadByte();
                if (ComRxPacket.DataPacketed(rx))
                {
                    if (ComRxPacket.NeedCommand == 0x03)
                    {
                        timer1.Enabled = false;
                        UInt16 weight = BytesOP.MakeShort(ComRxPacket.Data[6], ComRxPacket.Data[5]);
                        textBox31.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[8], ComRxPacket.Data[7]);
                        textBox25.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[10], ComRxPacket.Data[9]);
                        textBox21.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[12], ComRxPacket.Data[11]);
                        weight = (UInt16)(weight / 10);
                        textBox22.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[14], ComRxPacket.Data[13]);
                        textBox23.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[16], ComRxPacket.Data[15]);
                        weight = (UInt16)(weight / 10);
                        textBox24.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[18], ComRxPacket.Data[17]);
                        weight = (UInt16)(weight / 10);
                        textBox26.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[20], ComRxPacket.Data[19]);
                        textBox27.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[22], ComRxPacket.Data[21]);
                        weight = (UInt16)(weight / 10);
                        textBox28.Text = weight.ToString();
                        weight = ComRxPacket.Data[23];
                        textBox29.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[25], ComRxPacket.Data[24]);
                        textBox30.Text = weight.ToString();

                        ComRxPacket = new CP1616Packet(0x01, 0x0001);
                        byte[] txbuffer = CP1616Packet.MakeCP1616Packet(0x01, 0x0001, 0x0);
                        Com.Write(txbuffer, 0, txbuffer.Length);
                        index = 0;
                    }
                    else
                    {
                        index++;
                        int xx=0;
                        textBox18.Text = index.ToString();
                        UInt16 weight = BytesOP.MakeShort(ComRxPacket.Data[1], ComRxPacket.Data[0]);
                        textBox1.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[3], ComRxPacket.Data[2]);
                        textBox2.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[5], ComRxPacket.Data[4]);
                        textBox3.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[7], ComRxPacket.Data[6]);
                        textBox4.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[9], ComRxPacket.Data[8]);
                        textBox5.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[11], ComRxPacket.Data[10]);
                        textBox6.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[13], ComRxPacket.Data[12]);
                        textBox7.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[15], ComRxPacket.Data[14]);
//                         textBox8.Text = weight.ToString();
//                         weight = BytesOP.MakeShort(ComRxPacket.Data[17], ComRxPacket.Data[16]);
//                         textBox9.Text = weight.ToString();
//                         weight = BytesOP.MakeShort(ComRxPacket.Data[19], ComRxPacket.Data[18]);
//                         textBox10.Text = weight.ToString();
//                         weight = BytesOP.MakeShort(ComRxPacket.Data[21], ComRxPacket.Data[20]);
//                         textBox11.Text = weight.ToString();
//                         weight = BytesOP.MakeShort(ComRxPacket.Data[23], ComRxPacket.Data[22]);
//                         textBox12.Text = weight.ToString();
//                         weight = BytesOP.MakeShort(ComRxPacket.Data[25], ComRxPacket.Data[24]);
//                         textBox13.Text = weight.ToString();
//                         weight = BytesOP.MakeShort(ComRxPacket.Data[27], ComRxPacket.Data[26]);
//                         textBox14.Text = weight.ToString();
//                         weight = BytesOP.MakeShort(ComRxPacket.Data[29], ComRxPacket.Data[28]);
//                         textBox15.Text = weight.ToString();
//                         weight = BytesOP.MakeShort(ComRxPacket.Data[31], ComRxPacket.Data[30]);
//                         textBox16.Text = weight.ToString();

//                         textBox19.Text = ComRxPacket.Data[32].ToString();
//                         chart2.Series[0].Points.AddY(ComRxPacket.Data[32]);
// 
//                         weight = BytesOP.MakeShort(ComRxPacket.Data[33], ComRxPacket.Data[34]);
//                         textBox17.Text = weight.ToString();
// 
//                         weight = BytesOP.MakeShort(ComRxPacket.Data[35], ComRxPacket.Data[36]);
//                         textBox20.Text = weight.ToString();
// 
//                         chart1.Series[0].Points.AddY(weight);
//                         ComRxPacket = new CP1616Packet(0x01, 0x0001);

                        textBox19.Text = ComRxPacket.Data[16].ToString();
                        chart2.Series[0].Points.AddY(ComRxPacket.Data[16]);

                        weight = BytesOP.MakeShort(ComRxPacket.Data[17], ComRxPacket.Data[18]);
                        textBox17.Text = weight.ToString();

                        weight = BytesOP.MakeShort(ComRxPacket.Data[19], ComRxPacket.Data[20]);
                        textBox20.Text = weight.ToString();

                        chart1.Series[0].Points.AddY(weight);
                        ComRxPacket = new CP1616Packet(0x01, 0x0001);
                    }
                }
            }
        }

        private void label24_Click(object sender, EventArgs e)
        {

        }

        private void textBox21_TextChanged(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                byte[] txbuffer = CP1616Packet.MakeCP1616Packet(0x03, 0x0001, 0x0);
                Com.Write(txbuffer, 0, txbuffer.Length);
            }
            catch
            {
            	
            }
            
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Com.Close();
        }
    }
}
